package com.like.fragment;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bokecc.sdk.mobile.play.DWMediaPlayer;
import com.like.entity.CourseInfo;
import com.like.entity.EventWrapper;
import com.like.hospitalworld.R;
import com.like.hospitalworld.VideoCourseDetailActivity;
import com.like.utils.Constants;
import com.like.widget.MyToast;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.fragment_video_course_detail_content)
public class VideoCourseDetailContentFragment extends BaseFragment implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    @ViewInject(R.id.video_container)
    SurfaceView mSurface;
    @ViewInject(R.id.play_icon)
    ImageView mPlayIcon;
    @ViewInject(R.id.main_container)
    ViewGroup mMainContainer;

    private SurfaceHolder mSurfaceHolder;
    private DWMediaPlayer mPlayer;
    private CourseInfo mCourseInfo;
    private boolean mVideoPrepared = false;
    private int mCurrentPage = 0;
    private int mPageCount = 15;

    @Override
    protected void onCreateView() {
        super.onCreateView();
        mMainContainer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                EventBus.getDefault().post(new EventWrapper(null, VideoCourseDetailActivity.class, VideoCourseDetailActivity.REQUEST_CLOSE_COMMENT));
                return false;
            }
        });
        initSurface();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        release();
    }

    @Event({R.id.video_container, R.id.play_icon})
    private void videoClick(View v) {
        if (mPlayer == null || !mVideoPrepared) {
            MyToast.showBlueCircleToast(m.mContext, "视屏正在加载中");
            return;
        }
        if (mPlayer.isPlaying()) {
            mPlayer.pause();
            mPlayIcon.setVisibility(View.VISIBLE);
        } else {
            mPlayer.start();
            mPlayIcon.setVisibility(View.GONE);
        }
    }

    private void release() {
        if (mPlayer != null) {
            mPlayer.reset();
            mPlayer = null;
            mVideoPrepared = false;
        }
    }

    private void initSurface() {
        mSurfaceHolder = mSurface.getHolder();
        mSurfaceHolder.addCallback(this);
    }

    public void setCourseInfo(CourseInfo courseInfo) {
        this.mCourseInfo = courseInfo;
        mPlayer.setVideoPlayInfo(courseInfo.videoId + "", Constants.VIDEO_USER_ID, Constants.VIDEO_KEY, m.mContext);
        mPlayer.prepareAsync();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mPlayer = new DWMediaPlayer();
        mPlayer.setDisplay(mSurfaceHolder);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnErrorListener(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        mPlayer.setVideoPlayInfo(mCourseInfo.videoId + "", Constants.VIDEO_USER_ID_2, Constants.VIDEO_KEY_2, m.mContext);
        mPlayer.prepareAsync();
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mVideoPrepared = true;
    }
}
